package pl.lodz.p.model;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Transient;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
public class Visit {

	@Id
	@GeneratedValue
	private int id;
	
	@Column
	private Date dateStart;

	/**
	 * time in minutes
	 */
	@Column
	private int time;
	
	@ManyToOne
	@PrimaryKeyJoinColumn
	private Schedule schedule;
	
	@OneToMany(mappedBy="visit", cascade=CascadeType.ALL)
	private List<Notification> notifications;
	
	@ManyToOne
	@PrimaryKeyJoinColumn
	private Patient patient;
	
	@Transient
	private VisitDetails details;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	/**
	 * time in minutes
	 * @return
	 */
	int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}

	public Schedule getSchedule() {
		return schedule;
	}

	public void setSchedule(Schedule schedule) {
		this.schedule = schedule;
	}

	public Date getEndDate() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getDateStart());
		cal.add(Calendar.MINUTE, getTime());
		Date d = cal.getTime();
		return d;
	}

	public List<Notification> getNotifications() {
		return notifications;
	}

	public void setNotifications(List<Notification> notifications) {
		this.notifications = notifications;
	}

	public VisitDetails getDetails() {
		return details;
	}

	public void setDetails(VisitDetails details) {
		this.details = details;
	}
	
	public String getPatientName(){
		if (this.patient == null){
			return "nie zapisany";
		}
		return this.patient.getFirstName() + " " + this.patient.getLastName();
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}
}
